#include "stm32f10x.h"                  // Device header

void Timer_EncoderInit(void)
{
   //初始化外设时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//定时器时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//IO口时钟
	
	//初始化IO口
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//初始化定时器(由于时钟信号是外设编码器，因此无需配置时钟源)
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//由编码器托管，理论上不用配置
	TIM_TimeBaseInitStructure.TIM_Period=65535;//ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler=0;//不分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0x00;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//时基配置完成
	
	//初始化输入捕获
	TIM_ICInitTypeDef TIM_ICInitStructure;
	TIM_ICStructInit(&TIM_ICInitStructure);
	
	TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
	TIM_ICInitStructure.TIM_ICFilter=0XF;
	TIM_ICInit(TIM3,&TIM_ICInitStructure);	
	
	TIM_ICInitStructure.TIM_Channel=TIM_Channel_2;
	TIM_ICInitStructure.TIM_ICFilter=0XF;	
	TIM_ICInit(TIM3,&TIM_ICInitStructure);
	
	//配置编码器接口
	TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);    
	
	TIM_Cmd(TIM3,ENABLE);//使能能定时器
}

uint32_t GetCNT(void)
{
   return (int16_t)TIM_GetCounter(TIM3);  
}








